import resources.Constants;
import registnet.dao.*;

RegistNetPropertyBundle bundle = RegistNetPropertyBundle.getInstance("/resources/registnet");


String _record = bundle.getProperty("registnet.module.logs.form.record");
String _date = bundle.getProperty("registnet.module.logs.form.date");
String _time = bundle.getProperty("registnet.module.logs.form.time");
String _logDetails = bundle.getProperty("registnet.module.logs.form.log_details");
String _check = bundle.getProperty("registnet.messages.check");
Object log;
List fields;

private class LogRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		for(int i=0; i<data.length; i++){
			new Label(data[i]).setParent(row);	
		}
	}
}

void retrieveLogs(){
	try{
		String con = constraint.getSelectedItem().getLabel();
		String recordValue = record.getSelectedItem().getLabel();
		
		if(recordValue.equals("College")){
			fields = new College().getFields();
			log = new CollegeLogPeer();
		}
		else if(recordValue.equals("Unit")){
			fields = new Unit().getFields();
			log = new UnitLogPeer();
		}
		else if(recordValue.equals("Course")){
			fields = new Course().getFields();
			log = new CourseLogPeer();
		}
		else if(recordValue.equals("Degree Program")){
			fields = new DegreeCourse().getFields();
			log = new DegreeCourseLogPeer();
		}
		else if(recordValue.equals("Curriculum")){
			fields = new Curriculum().getFields();
			log = new CurriculumLogPeer();
		}
		else if(recordValue.equals("Student")){
			fields = new Student().getFields();
			log = new StudentLogPeer();
		}
		else if(recordValue.equals("Grade")){
			fields = new Grade().getFields();
			log = new GradeLogPeer();
		}
		else if(recordValue.equals("System")){
			log = new SystemLogPeer();
		}
		
		List logs;
		
		if(con.equals("Record")){
			logs = log.getAllLogs();
		}
		else if(con.equals("Year")){
			logs = getLogsByYear();
		}
		else if(con.equals("Month")){
			logs = getLogsByMonth();
		}
		else if(con.equals("Day")){
			logs = getLogsByDay();
		}
		else if(con.equals("Time")){
			logs = getLogsByTime();
		}
		
		if(recordValue.equals("System")){
			viewSystemLog(logs);
			return;
		}
		
		int fc = 0; 
		for(int i=0; i<logs.size(); i++)
			if(logs.get(i).getAction()==4)
				fc++;
		
		String[][] listModel = new String[logs.size()-fc][5];
		int i=0;
		for(j = 0; j < logs.size(); j++){
			Object obj = logs.get(j);
			int action = obj.getAction();
			if(action!=4){
				listModel[i][0] = obj.getMonth()+"-"+obj.getDay()+"-"+obj.getYear();
				listModel[i][1] = obj.getHour()+":"+obj.getMinute()+":"+obj.getSecond();
				HashMap actionMap;
				actionMap = Constants.MAP_LOG_ACTION_VALUE;
				String[] attrib = obj.getAttributes();
				String msg;
				if(action==1){
					msg = actionMap.get(action) + " new " + recordValue.toUpperCase() +" " +obj.getIdentifiers();
					if(log instanceof GradeLogPeer){
						msg = msg + " Grade=\"" + obj.getGrade()+"\"";
					}
				}
				else if(action==2){
					String[] newVal = logs.get(j+1).getAttributes();
					String changes = getChanges(attrib, newVal);
					msg = actionMap.get(action)+ " " + recordValue.toUpperCase() + " " +obj.getIdentifiers()+ " "+changes;
				}
				else if(action==3){
					msg = actionMap.get(action)+ " " + recordValue.toUpperCase() + " " +obj.getIdentifiers();
				}
				listModel[i][2] = msg;
				listModel[i][3] = obj.getUser();
				listModel[i][4] = obj.getLogUnitCode();
				i++;
			}
		}
		ListModel strset = new SimpleListModel(listModel);
		contents_win.getFellow("logs_g").setModel(strset);
		contents_win.getFellow("logs_g").setRowRenderer(new LogRenderer());
		
	}catch(Exception e){
		e.printStackTrace();
	}
}

List getLogsByYear(){
	List logs;
	Integer y1 = year1.getValue();
	Integer y2 = year2.getValue();
	if(y1!=null){
		if(y2!=null){
			if(y2>=y1)
				logs = log.searchLogsByYear(y1, y2);
			else
				alert(_check);
		}
		else
			logs = log.getLogsByYear(y1);
	}
	else
		alert(_check);
	return logs;
}

List getLogsByMonth(){
	Integer year = year1.getValue();
	Integer m1 = month1.getSelectedIndex();
	Integer m2 = month2.getSelectedIndex();
	List logs;
	if(year!=null){
		if(m1!=-1){
			if(m2!=-1 && m2!=12){
				if(m2>=m1)
					logs = log.searchLogsByMonth(year,m1+1, m2+1);
				else
					alert(_check);
			}
			else
				logs = log.getLogsByMonth(year,m1+1);
		}
		else
			alert(_check);
	}
	else
		alert(_check);
	return logs;
}

List getLogsByDay(){
	
	List logs;
	Integer year;
	Integer month;
	Integer days1, days2;
	Date d1 = day1.getValue();
	Date d2 = day2.getValue(); 
	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
	
	if(d1!=null){
		String[] date1 = sdf.format(d1).split("-");
		year = Integer.parseInt(date1[0]);
		month = Integer.parseInt(date1[1]);
		days1 = Integer.parseInt(date1[2]);
		if(d2!=null){
			String[] date2 = sdf.format(d2).split("-");
			days2 = Integer.parseInt(date2[2]);
			if(d2.compareTo(d1)>=0){
				if(date1[0].equals(date2[0]) && date1[1].equals(date2[1])){
					logs = log.searchLogsByDay(year,month, days1, days2);
				}
				else
					alert("Date must have the same year and month.");
			}
			else
				alert(_check);
		}
		else
			logs = log.getLogsByDay(year, month, days1);
	}
	else
		alert(_check);
		
	return logs;
}


List getLogsByTime(){
	List logs;
	Integer year;
	Integer month;
	Integer day;
	
	Date d = day1.getValue();
	Integer t1 = time1.getValue();
	Integer t2 = time2.getValue();
	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
	
	if(d!=null){
		String[] date = sdf.format(d).split("-");
		year = Integer.parseInt(date[0]);
		month = Integer.parseInt(date[1]);
		day = Integer.parseInt(date[2]);
		
		if(t1!=null && t1<24){
			if(t2!=null){
				if(t2>=t1  && t2<24)
					logs = log.searchLogsByTime(year,month, day, t1, t2);
				else
					alert(_check);
			}
			else
				logs = log.getLogsByTime(year, month, day, t1);
		}
		else
			alert(_check);
	}
	else
			alert(_check);	
	return logs;
}


doSearch(String cons){
	record.setSelectedIndex(0);
	record.setVisible(true);
	recordV.setVisible(true);
	go.setVisible(true);
	
	if(cons.equals("Record")){
		year1.setVisible(false);
		year2.setVisible(false);
		month1.setVisible(false);
		month2.setVisible(false);
		day1.setVisible(false);
		day2.setVisible(false);
		time1.setVisible(false);
		time2.setVisible(false);
		
		timeTo.setVisible(false);
		yearTo.setVisible(false);
		monthTo.setVisible(false);
		dayTo.setVisible(false);
		
		yearV.setVisible(false);
		monthV.setVisible(false);
		dayV.setVisible(false);
		timeV.setVisible(false);
	}
	else if(cons.equals("Year")){
		year1.setVisible(true);
		year2.setVisible(true);
		month1.setVisible(false);
		month2.setVisible(false);
		day1.setVisible(false);
		day2.setVisible(false);
		time1.setVisible(false);
		time2.setVisible(false);
		
		timeTo.setVisible(false);
		yearTo.setVisible(true);
		monthTo.setVisible(false);
		dayTo.setVisible(false);
		
		yearV.setVisible(true);
		monthV.setVisible(false);
		dayV.setVisible(false);
		timeV.setVisible(false);
	}
	else if(cons.equals("Month")){
		month2.setSelectedIndex(12);
		
		year1.setVisible(true);
		year2.setVisible(false);
		month1.setVisible(true);
		month2.setVisible(true);
		day1.setVisible(false);
		day2.setVisible(false);
		time1.setVisible(false);
		time2.setVisible(false);
		
		timeTo.setVisible(false);
		yearTo.setVisible(false);
		monthTo.setVisible(true);
		dayTo.setVisible(false);
		
		yearV.setVisible(true);
		monthV.setVisible(true);
		dayV.setVisible(false);
		timeV.setVisible(false);
	}
	else if(cons.equals("Day")){
		year1.setVisible(false);
		year2.setVisible(false);
		month1.setVisible(false);
		month2.setVisible(false);
		day1.setVisible(true);
		day2.setVisible(true);
		time1.setVisible(false);
		time2.setVisible(false);
		
		timeTo.setVisible(false);
		yearTo.setVisible(false);
		monthTo.setVisible(false);
		dayTo.setVisible(true);
		
		yearV.setVisible(false);
		monthV.setVisible(false);
		dayV.setVisible(true);
		timeV.setVisible(false);
	}
	else if(cons.equals("Time")){
		
		year1.setVisible(false);
		year2.setVisible(false);
		month1.setVisible(false);
		month2.setVisible(false);
		day1.setVisible(true);
		day2.setVisible(false);
		time1.setVisible(true);
		time2.setVisible(true);
		
		timeTo.setVisible(true);
		yearTo.setVisible(false);
		monthTo.setVisible(false);
		dayTo.setVisible(false);
		
		yearV.setVisible(false);
		monthV.setVisible(false);
		dayV.setVisible(true);
		timeV.setVisible(true);
	}
}

String getChanges(String[] oldVal, String[] newVal){
	String changes="";
	for(int i=0; i<oldVal.length; i++){
		if(fields.get(i).equals("Photo")){
			//changes=changes+"Photo TO new photo ";
		}
		else if(fields.get(i).equals("Signature")){
			//changes=changes+"Signature TO new signature ";
		}
		else if(oldVal[i]!=null && newVal[i]!=null){
			if(oldVal[i].compareTo(newVal[i])!=0){
				changes = changes+fields.get(i)+"=\""+oldVal[i]+"\" TO "+"\""+newVal[i]+"\" ";
			}
		}
		else if((oldVal[i]==null&&newVal[i]!=null) || (oldVal[i]!=null&&newVal[i]==null))
			changes = changes+fields.get(i)+"=\""+oldVal[i]+"\" TO "+"\""+newVal[i]+"\" ";
	}
	return changes;
}

viewSystemLog(List logs){
	String[][] listModel = new String[logs.size()][5];
	for(i = 0; i < logs.size(); i++){
		Object obj = logs.get(i);
		listModel[i][0] = obj.getMonth()+"-"+obj.getDay()+"-"+obj.getYear();
		listModel[i][1] = obj.getHour()+":"+obj.getMinute()+":"+obj.getSecond();
		listModel[i][2] = obj.getLogMessage();
		listModel[i][3] = obj.getUser();
		listModel[i][4] = obj.getLogUnitCode();
	}
	ListModel strset = new SimpleListModel(listModel);
	contents_win.getFellow("logs_g").setModel(strset);
	contents_win.getFellow("logs_g").setRowRenderer(new LogRenderer());
}